<style scoped>
.wraper {
    padding: 0 30px;
}

.search {
    padding-bottom: 20px;
}

.search span {
    color: #68777c;
    font-weight: bold;
    padding-right: 18px;
}

.search button {
    background-color: #00a8f7;
    color: #fff;
    margin-left: 8px;
}

.top {
    margin-bottom: 20px;
    border-top: 1px dashed #cccccc;
}

.detail {
    border-top: 1px dashed #cccccc;
}

.wraper .headline {
    position: relative;
    font-size: 12px;
    color: #303134;
    font-weight: bold;
    margin: 10px 0;
    line-height: 32px;
}

.wraper .add-agent {
    position: absolute;
    top: 0px;
    right: 30px;
    background-color: #348bf2;
    color: #fff;
}

.iconfont {
    font-size: 14px;
}

.red-font {
    color: #ff1f1f;
}

/* 分页 start */

.page {
    margin: 20px 0;
}
.ivu-page {
    padding-bottom: 150px;
} 

/* 分页 end */
</style>
<template>
    <div>
        <product-title></product-title>
        <div class="wraper">
            <div class="search">
                <span>快速搜索</span>
                
                <Select v-model="searchForm.uid" style="width:140px" placeholder='请选择代理人'>
                    <Option value="0" >全部</Option>
                    <Option v-for="item in uidList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                </Select>           
                <Button @click='search'>查询</Button>
            </div>
            <div class="detail">
                <div class="headline">
                    <p>
                        <span class="red-font">提示</span>：
                    </p>
                    <Button class="add-agent" @click='goCreate()'>新增记录</Button>
                </div>
                <div class="clearfix">
                    <Table border :columns="columns_achievement" :data="data_achievement"></Table>
                    <Row class="page">
                        <Col span="24">
                        <Page :total="achievementTotal" show-total show-sizer :page-size="searchForm.pagesize" @on-change="pageChange($event)" @on-page-size-change="pageSizeChange($event)"></Page>
                        </Col>
                    </Row>
                </div>
            </div>
        </div>
        <Modal v-model="showAddModal" :mask-closable='closeable' width="600" title="添加/编辑员工绩效" @on-ok="add()" @on-cancel="cancel()" :loading="loading">
            <Row>
                <Col span="18" offset="1">
                <Form :model="achievement" ref="achievement" :rules="ruleValidate" :label-width="120">
                    <Form-item label="保单号：" prop="policy_no" required>
                        <Input v-model="achievement.policy_no" placeholder="请输入员工保单编号"></Input>
                    </Form-item>                    
                    <Form-item label="代理人：" prop="uid"  required>
                        <Select v-model="achievement.uid" style="width:306px" @on-change="agentChange($event)" placeholder='请选择代理人'>
                            <Option v-for="item in uidList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                        </Select>
                    </Form-item>
                    <Form-item label="投保人：">
                        <Select v-model="achievement.au_id"  @on-change="auchange($event)"  filterable style="width:306px" placeholder='请选择投保人'>
                            <!-- <Option v-for="item in auidList" :value="item.value" :key="item.value">{{ item.label }}</Option> -->
                            <Option v-for="(value,key) in auidList" :value="key" :key="key" :label="value">
                                <span>{{value}}</span>
                                <span style="float:right;color:#ccc">{{key}}</span>
                            </Option>
                        </Select>
                    </Form-item>
                    <Form-item label="产品：" prop="pid" required>
                        <Select v-model="achievement.pid" @on-change="prochange($event)" style="width:306px" placeholder='请选择产品'>
                            <Option v-for="(value,key) in proList" :value="key" :key="key">{{ value.name }}</Option>
                        </Select>
                    </Form-item>
                    <Form-item label="交费时间：" prop="pay_time"  required>
                        <DatePicker type="date" v-model="achievement.pay_time_date" @on-change="timechange($event)" placeholder="选择交费日期" ></DatePicker>
                    </Form-item>
                    <Form-item label="交费年次：" prop="pay_num" required>
                        <Input v-model="achievement.pay_num" placeholder="请输入交费年次"></Input>
                    </Form-item>
                    <Form-item label="保险期间：" prop="period" required>
                        <Select v-model="achievement.period" style="width:306px" placeholder='请选择保险期间'>
                            <Option v-for="item in periodList" :value="item" :key="item">{{ item }}</Option>
                        </Select>
                    </Form-item>
                    <Form-item label="交费期间：" prop="pay_period" required>
                        <Select v-model="achievement.pay_period" style="width:306px" placeholder='请选择交费期间'>
                            <Option v-for="item in pay_periodList" :value="item" :key="item">{{ item }}</Option>
                        </Select>
                    </Form-item>
                    <Form-item label="保费：" prop="premium" required>
                        <Input v-model="achievement.premium" placeholder="请输入保费"></Input>
                    </Form-item>                 
                </Form>
                </Col>
            </Row>
        </Modal>
    </div>
</template>
<script>
import productTitle from '../../components/productTitle'
import { getProListAll } from '../../api/product_v2'
import { getCustomerAllById } from '../../api/customer'
import { createAchi , getAchiList , delAchi ,updateAchi } from '../../api/achievement'
import { getAllEmployee } from '../../api/employee'
export default {
    data () {
        const validatepremium = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('保费不能为空'));
            } else {               
                callback();
            }
        };
        return {
            searchForm: {
                uid:0,
                p: 1,
                pagesize: 10
            },
            uoc:-1,
            closeable: false,
            loading: true,
            showAddModal:false,
            achievementTotal:0,
            achievement: {
                au_id:'',
                au_name:'',
            },
            uidList:[
            ],
            auidList:{},
            proList:{},
            periodList:[],
            pay_periodList:[],
            ruleValidate: {
                policy_no: [
                    { required: true, message: '保单号不能为空', trigger: 'blur' }
                ],
                uid: [
                    { required: true, message: '代理人不能为空', trigger: 'change' },
                ],
            
                pid: [
                    { required: true, message: '产品不能为空', trigger: 'change' }
                ],
                pay_time: [
                    { required: true, message: '交费时间不能为空', trigger: 'change' }
                ],
                pay_num: [
                    { required: true, message: '交费年限不能为空', trigger: 'blur' }
                ],
                period: [
                    { required: true, message: '保险期间不能为空', trigger: 'change' }
                ],
                pay_period: [
                    { required: true, message: '交费期间不能为空', trigger: 'change' }
                ],                
                premium: [
                    { required: true,validator: validatepremium, trigger: 'blur' }
                ], 
            },
            columns_achievement: [
                {
                    title: '保单号',
                    key: 'policy_no',
                    align: 'center',
                },
                {
                    title: '代理人',
                    key: 'agent_name',
                    align: 'center',
                },
                {
                    title: '投保人',
                    key: 'au_name',
                    align: 'center',
                },
                {
                    title: '产品',
                    key: 'pname',
                    align: 'center',
                }, 
                {
                    title: '交费时间',
                    key: 'pay_time',
                    align: 'center',
                },
                 {
                    title: '保费',
                    key: 'premium',
                    align: 'center',
                },
                 {
                    title: '佣金',
                    key: 'fee_amount',
                    align: 'center',
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 150,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [                            
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.goUpdate(params.row.id)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        this.delConfirm(params.index)
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                },
            ],
            data_achievement: [

            ],
        }
    },
    components: {
        productTitle
    },
    created() {
        this.getachi()
        this.getprolist()
        this.getAgent()

    },
    methods: {
        goUpdate(id){
            this.$router.push({'name':'achievement_update',params:{id:id}})
        },
        goCreate(){
            this.$router.push({'name':'achievement_create'})
        },
        // 搜索
        search() {
            this.getachi();
        },
        pageChange(){

        },
        pageSizeChange(){

        },
        agentChange(value) {
            this.getCustomer()
        },
        //获取业绩列表
        getachi(){           
            new Promise((resolve, reject) => {
                getAchiList(this.searchForm).then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {
                        this.data_achievement = response.data.data.list
                        this.achievementTotal = response.data.data.total
                    } else {
                        this.$Message.error(response.data.info);
                        
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //获取产品
        getprolist(){           
            new Promise((resolve, reject) => {
                getProListAll().then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {
                        this.proList = response.data.data
                    } else {
                        this.$Message.error(response.data.info);
                        
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //获取客户
        getCustomer() {
            let _param = {
                agent_uid:this.achievement.uid
            }
            new Promise((resolve, reject) => {
                getCustomerAllById(_param).then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {
                        this.auidList = response.data.data
                        //this.$set(this,'auidList',response.data.data)
                    } else {
                        //this.$Message.error(response.data.info);
                        
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        
        //获取代理人
        getAgent() {
            new Promise((resolve, reject) => {
                getAllEmployee().then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {
                        this.uidList = response.data.data
                    } else {
                        this.$Message.error(response.data.info);
                        
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        auchange(value) {
            //console.log(value)
            //console.log(auidList)
            this.achievement.au_name = this.auidList[value]
            //console.log(this.achievement.au_name)
        },
        prochange(value){
            this.periodList = this.proList[value].period
            this.pay_periodList = this.proList[value].pay_period
            this.achievement.pname = this.proList[value].name
        },
        timechange(value){
            console.log(value)
            this.achievement.pay_time = value
        },

        //添加or编辑代理人，打开modal
        showAdd(index) {
            this.showAddModal = !this.showAddModal;
            if (index != -1) {
                
                //this.getDetail(params); 
                console.log(this.data_achievement[index])                
                this.achievement = Object.assign({}, this.data_achievement[index] );
                console.log(this.achievement)
                this.achievement.uid = this.achievement.uid+''
                this.achievement.au_id = this.achievement.au_id+''
                this.achievement.pay_time_date = this.data_achievement[index].pay_time
                this.uoc = 1
            } else {
                //this.achievement = {};
                this.$refs['achievement'].resetFields()
                this.uoc = -1
            }
        },
        add() {
            this.$refs['achievement'].validate((valid) => {
                if (valid) {
                    if(this.uoc == -1){
                        new Promise((resolve, reject) => {
                            createAchi(this.achievement).then(response => {
                                if (response.data.err == 0) {
                                    this.showAddModal = false;
                                    this.loading = true;
                                    this.getachi();
                                    this.$Message.success(response.data.info);
                                } else {
                                    this.$Message.error(response.data.info);
                                    setTimeout(() => {
                                        this.loading = false;
                                        this.$nextTick(() => {
                                            this.loading = true;
                                        });
                                    }, 2000);
                                }
                                resolve();
                            }).catch(error => {
                                
                                setTimeout(() => {
                                    this.loading = false;
                                    this.$nextTick(() => {
                                        this.loading = true;
                                    });
                                }, 2000);
                                reject(error);
                            });
                        });
                    }else{
                        new Promise((resolve, reject) => {
                            updateAchi(this.achievement).then(response => {
                                if (response.data.err == 0) {
                                    this.showAddModal = false;
                                    this.loading = true;
                                    this.getachi();
                                    this.$Message.success(response.data.info);
                                } else {
                                    this.$Message.error(response.data.info);
                                    setTimeout(() => {
                                        this.loading = false;
                                        this.$nextTick(() => {
                                            this.loading = true;
                                        });
                                    }, 2000);
                                }
                                resolve();
                            }).catch(error => {
                                
                                setTimeout(() => {
                                    this.loading = false;
                                    this.$nextTick(() => {
                                        this.loading = true;
                                    });
                                }, 2000);
                                reject(error);
                            });
                        });
                    }                    
                } else {
                    // 通过loading来阻止modal隐藏，然后手动修改loading来充值确定按钮
                    setTimeout(() => {
                        this.loading = false;
                        this.$nextTick(() => {
                            this.loading = true;
                        });
                    }, 2000);
                }
            })
        },
        cancel() {
            this.$refs['achievement'].resetFields()
        },
        del(param) {
            new Promise((resolve, reject) => {
                delAchi(param).then(response => {
                    this.$Message.success(response.data.info);
                    if (response.data.err == 0) {
                        this.getachi()
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        // 删除时弹出对话框
        delConfirm(index) {
            this.$Modal.confirm({
                title: '提示',
                content: '是否要删除该条记录?',
                onOk: () => {
                    let _params = {
                        'id': this.data_achievement[index].id
                    }
                    this.del(_params);
                },
            });
        },
        pageChange(page) {
            this.searchForm.p = page
            this.getachi()
        },
        pageSizeChange(pagesize) {
            console.log(pagesize)
            this.searchForm.pagesize = pagesize
            this.getachi()
        }
    }
}
</script>
